package com.netdisk.component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.netdisk.Domain.User;
import com.netdisk.service.IUserService;
import com.qiwenshare.common.constant.RegexConstant;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Pattern;

@Component
public class UserDealComp {
    @Resource
    IUserService userService;


    /**
     * 检测用户名是否存在
     *
     * @param userBean
     */
    public Boolean isUserNameExit(User userBean) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, userBean.getUsername());
        List<User> list = userService.list(lambdaQueryWrapper);
        return list != null && !list.isEmpty();
    }

    /**
     * 检测手机号是否存在
     *
     * @param userBean
     * @return
     */
    public Boolean isPhoneExit(User userBean) {

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getTelephone, userBean.getTelephone());
        List<User> list = userService.list(lambdaQueryWrapper);
        return list != null && !list.isEmpty();

    }

    public Boolean isPhoneFormatRight(String phone){
        boolean isRight = Pattern.matches(RegexConstant.PASSWORD_REGEX, phone);
        return isRight;
    }
}
